﻿package core.drawing
{
    import core.enum.*;
    import core.image.*;
    import core.ui.*;
    import flash.events.*;
    import flash.geom.*;

    public class SelectionTool extends Tool
    {
        var pt:Point;
        var pr:Point;
        var po:Point;
        var pe:Point;

        public function SelectionTool(param1:Tuyitu, param2:ToolType)
        {
            super(param1, param2, true);
            param1.stage.addEventListener(KeyboardEvent.KEY_DOWN, this.KeyDown, false, 0, true);
            param1.stage.addEventListener(KeyboardEvent.KEY_UP, this.KeyUp, false, 0, true);
            return;
        }// end function

        override public function MouseMove(event:MouseEvent) : void
        {
            if (ed.canvas.selection != null)
            {
                if (ed.canvas.selection.IsSelected(ed.canvas.document.mouseX, ed.canvas.document.mouseY))
                {
                    if (type.attitude.method == "normal")
                    {
                        type.attitude.method = "move";
                        ed.cursor.Set(type);
                    }
                }
                else if (type.attitude.method == "move")
                {
                    if (event.shiftKey)
                    {
                        type.attitude.method = "add";
                    }
                    else if (event.ctrlKey)
                    {
                        type.attitude.method = "subtract";
                    }
                    else
                    {
                        type.attitude.method = "normal";
                    }
                    ed.cursor.Set(type);
                }
            }
            return;
        }// end function

        public function SelectionDown() : void
        {
            history.Prepare(Action.SelectionMove);
            ed.cursor.freeze = true;
            canvas.back.removeEventListener(MouseEvent.MOUSE_MOVE, this.MouseMove, false);
            ed.addEventListener(MouseEvent.MOUSE_MOVE, this.SelectionDrag, false, 1, true);
            ed.addEventListener(MouseEvent.MOUSE_UP, this.SelectionUp, false, 1, true);
            this.pt = canvas.GetMousePosition();
            return;
        }// end function

        public function SelectionDrag(event:MouseEvent) : void
        {
            this.po = canvas.GetMousePosition();
            this.pt.x = this.po.x - this.pt.x;
            this.pt.y = this.po.y - this.pt.y;
            selection.Move(this.pt);
            this.pt = this.po;
            canvas.coating.Clear();
            canvas.coating.Redraw();
            return;
        }// end function

        public function SelectionUp(event:MouseEvent) : void
        {
            var _loc_2:Canvas = null;
            var _loc_3:int = 0;
            var _loc_4:Selection = null;
            ed.cursor.freeze = false;
            canvas.back.addEventListener(MouseEvent.MOUSE_MOVE, this.MouseMove, false, 0, true);
            ed.removeEventListener(MouseEvent.MOUSE_MOVE, this.SelectionDrag, false);
            ed.removeEventListener(MouseEvent.MOUSE_UP, this.SelectionUp, false);
            if (selection != null && (selection.ox != 0 || selection.oy != 0))
            {
                if (ed.canvas.masking.hitTestPoint(ed.stage.mouseX, ed.stage.mouseY))
                {
                    selection.ResetOffset();
                    ed.navigator.SetSelectionSize(selection.width, selection.height);
                    history.Commit();
                }
                else
                {
                    selection.ox = 0;
                    selection.oy = 0;
                    _loc_3 = ed.area.numChildren - 1;
                    while (_loc_3 >= 0)
                    {
                        
                        if ((ed.area.getChildAt(_loc_3) as Canvas).masking.hitTestPoint(ed.stage.mouseX, ed.stage.mouseY))
                        {
                            _loc_2 = ed.area.getChildAt(_loc_3) as Canvas;
                            if (_loc_2 != ed.canvas)
                            {
                                _loc_4 = selection.Clone();
                                ed.ActivateCanvas(_loc_2);
                                _loc_2.SetSelection(_loc_4);
                            }
                            break;
                        }
                        _loc_3 = _loc_3 - 1;
                    }
                }
                ed.canvas.coating.Clear();
                ed.canvas.coating.Redraw();
            }
            if (!AppState.OnCanvas)
            {
                ed.cursor.Set(ToolType.None);
            }
            return;
        }// end function

        override public function CleanUp() : void
        {
            ed.stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.KeyDown, false);
            ed.stage.removeEventListener(KeyboardEvent.KEY_UP, this.KeyUp, false);
            type.attitude.method = "normal";
            return;
        }// end function

        public function KeyDown(event:KeyboardEvent) : void
        {
            if (event.keyCode == 16)
            {
                type.attitude.method = "add";
                if (AppState.OnCanvas)
                {
                    ed.cursor.Set(type, ed.cursor.freeze);
                }
            }
            else if (event.keyCode == 17)
            {
                type.attitude.method = "subtract";
                if (AppState.OnCanvas)
                {
                    ed.cursor.Set(type, ed.cursor.freeze);
                }
            }
            return;
        }// end function

        public function KeyUp(event:KeyboardEvent) : void
        {
            if (!event.shiftKey && !event.ctrlKey)
            {
                if (canvas != null && ed.canvas.selection != null && ed.canvas.selection.IsSelected(ed.canvas.document.mouseX, ed.canvas.document.mouseY))
                {
                    type.attitude.method = "move";
                }
                else
                {
                    type.attitude.method = "normal";
                }
            }
            else if (event.shiftKey)
            {
                type.attitude.method = "add";
            }
            else if (event.ctrlKey)
            {
                type.attitude.method = "subtract";
            }
            if (AppState.OnCanvas)
            {
                ed.cursor.Set(type, ed.cursor.freeze);
            }
            return;
        }// end function

        override public function Reset() : void
        {
            type.attitude.method = "normal";
            return;
        }// end function

    }
}
